package com.client.qiuqiu;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

public class Register_page extends AppCompatActivity {
    String registerState = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_page);

        //按钮点击事件
        Button registerButn;
        Button returnButn;

        returnButn = (Button) findViewById(R.id.buttonReturn);
        registerButn = (Button) findViewById(R.id.buttonRegister);

        returnButn.setOnClickListener(new Return());
        registerButn.setOnClickListener(new Register());
    }

    public void setRegisterState(String registerState) {
        this.registerState = registerState;
    }

    class Register implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            register_Thread register_thread = new register_Thread();
            register_thread.start();

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(registerState.equals("inconsistent")){
                Toast.makeText(Register_page.this, "The passwords are inconsistent!!", Toast.LENGTH_LONG).show();
            }else if(registerState.equals("NullName")){
                Toast.makeText(Register_page.this, "The name is empty!!", Toast.LENGTH_LONG).show();
            }else if(registerState.equals("NullPwd")){
                Toast.makeText(Register_page.this, "The password is empty!!", Toast.LENGTH_LONG).show();
            }else if(registerState.equals("Wrong")){
                Toast.makeText(Register_page.this, "Something Wrong!!\nQAQ", Toast.LENGTH_LONG).show();
            }else{
                AlertDialog.Builder builder = new AlertDialog.Builder(Register_page.this);
                builder.setTitle("Your ID");
                builder.setMessage(String.format("Your ID is %s. \nPlease remember your ID.", registerState));
                builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent (Register_page.this, Login_page.class);
                        startActivity(intent);
                        finish();
                    }
                });
                builder.show();
            }
        }
    }

    class Return implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent (Register_page.this, Login_page.class);
            startActivity(intent);
            finish();
        }
    }

    class register_Thread extends Thread{
        public void run(){
            EditText name = findViewById(R.id.rigesterUserName);
            EditText pwd = findViewById(R.id.rigesterUserPwd);
            EditText pwdComfirm = findViewById(R.id.rigesterUserPwdComfirm);

            String userName = name.getText().toString();
            String userPwd = pwd.getText().toString();
            String userPwdComfirm = pwdComfirm.getText().toString();

            //姓名为空
            if(userName.trim().isEmpty() || userName.equals("false")){
                setRegisterState("NullName");
                return;
            }

            //密码为空
            if(userPwd.trim().isEmpty()){
                setRegisterState("NullPwd");
                return;
            }

            //密码与确认不符，警告
            if(!userPwd.equals(userPwdComfirm)){
                setRegisterState("inconsistent");
                return;
            }

            User user = ((User)getApplicationContext());


            try {
                //获取服务器端的输出流
                PrintStream printStream = new PrintStream(user.getSocket().getOutputStream());
                printStream.println("Register");
                //传name
                printStream.println(userName);
                //pwd加密，then传
                userPwd = Sha256.sha256(userPwd);
                printStream.println(userPwd);

            } catch (IOException e) {
                e.printStackTrace();
            }

            //获取输入流
            String ID = null;
            try {
                Scanner scanner = new Scanner(user.getSocket().getInputStream());
                ID = scanner.nextLine();

                if(!ID.equals("0")){
                    setRegisterState(ID);
                    return;
                }else{
                    setRegisterState("Wrong");
                    return;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}